package com.kmproject.haifu.mvp.ui.adapter;

import android.content.pm.ActivityInfo;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.integration.AppManager;
import com.jess.arms.utils.ArmsUtils;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.Glide4Engine;
import com.kmproject.haifu.mvp.ui.activity.HousePushUpdateImgActivity;
import com.kmproject.haifu.mvp.ui.widget.Subject;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.util.List;

public class AddImgAdapter extends BaseQuickAdapter<Subject,BaseViewHolder> {
    private final int FIRST_TOP_VIEW=1000;
    ImageLoader glideImage;
    private int maxNumImg=20;
    private boolean isShowDelImg=true;
    public AddImgAdapter(@Nullable List<Subject> data) {
        super(R.layout.add_img_item,data);

    }



    public void setShowDelImg(boolean showDelImg) {
        isShowDelImg = showDelImg;
    }

    public void setMaxNumImg(int maxNumImg) {
        this.maxNumImg = maxNumImg;
    }

    @Override
    protected void convert(BaseViewHolder helper, Subject item) {
        if (glideImage == null) {
            glideImage = ArmsUtils.obtainAppComponentFromContext(mContext).imageLoader();
        }
        if (!isShowDelImg){
            helper.<ImageView>getView(R.id.add_img_item_del).setVisibility(View.GONE);
        }
        if (helper.getPosition()==getItemCount()-1)
        {
            helper.<ImageView>getView(R.id.add_img_item_img).setImageResource(R.drawable.update_img_add_icon);
            helper.<ImageView>getView(R.id.add_img_item_del).setVisibility(View.GONE);

            helper.<ImageView>getView(R.id.add_img_item_img).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getItemCount()-1==maxNumImg)
                    {
                        ArmsUtils.makeText(mContext,"最多只能选取"+maxNumImg+"张图片");
                        return;
                    }
                    //拍照界面
                    Matisse.from(AppManager.getAppManager().getCurrentActivity())
                            .choose(MimeType.ofImage())
                            .countable(true)
                            .capture(true)
                            .captureStrategy(
//                new CaptureStrategy(true, "com.lxc.inspectionmanagement.fileprovider"))
                                    new CaptureStrategy(true, "com.kmproject.haifu.fileprovider"))
                            .maxSelectable(maxNumImg-(getItemCount()-2))
                            .gridExpectedSize(AppManager.getAppManager().getCurrentActivity().getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                            .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                            .thumbnailScale(0.85f)
                            .imageEngine(new Glide4Engine())
                            .forResult(AppConstant.REQUEST_CODE_CHOOSE);
                }
            });
        }else {
            helper.<ImageView>getView(R.id.add_img_item_del).setVisibility(View.VISIBLE);
           if (item.getServerPath()!=null&&!TextUtils.isEmpty(item.getServerPath()))
           {
               //显示服务器
               glideImage.loadImage(mContext, ImageConfigImpl.builder().url(item.getServerPath()).imageRadius(5)
                       .imageView(helper.<ImageView>getView(R.id.add_img_item_img))
                       .build());
           }else {
               //显示本地的
               glideImage.loadImage(mContext, ImageConfigImpl.builder().url(item.getFile().getPath()).imageRadius(5)
                       .imageView(helper.<ImageView>getView(R.id.add_img_item_img))
                       .build());
           }

            helper.addOnClickListener(R.id.add_img_item_img);
        }


    }

}
